<?php
class TwitterService
{
  
  public $requestCount=100;
  public $milesFromDestination=5;
  
  const TWITTER_SEARCH_URL = '';
  
  function __construct(){}
  
  public function getGeoTweets($lat, $lng){
    $tweetUrl = "http://search.twitter.com/search.json?rpp=" . $this->requestCount . "&geocode=" . $lat . "," . $lng . "," . $this->milesFromDestination . "mi";
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, $tweetUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $twitterResponse = curl_exec ($curl);
    curl_close ($curl);
    return $this->getTweets(json_decode($twitterResponse, true));
  }
  
  private function getTweets($rawJSON ){
    
    $result = new TwitterVO();
    
    for ($i = 0; $i < count($rawJSON["results"]);$i++){
      $rawItem = $rawJSON["results"][$i];
      $tweetVO = new TweetVO();
      $tweetVO->from_user = $rawItem["from_user"];
      $tweetVO->location = $rawItem["location"];
      $tweetVO->profile_image_url = $rawItem["profile_image_url"];
      $tweetVO->text = $rawItem["text"];
      
      if(!is_null($rawItem["geo"])){
        $tweetVO->lat = $rawItem["geo"]["coordinates"][0];
        $tweetVO->lng = $rawItem["geo"]["coordinates"][1];
        array_push($result->geoTweetList, $tweetVO);
      }
      array_push($result->tweetList, $tweetVO);
    }
    
    return $result;
  }
}
?>